package com.wangyadong.hobby.common.aop;

import com.wangyadong.hobby.common.utils.BaseControllerConfig;
import com.wangyadong.hobby.front.model.WuserInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class LogAspect extends BaseControllerConfig {
    private static Logger LOGGER = LoggerFactory.getLogger(BaseControllerConfig.class);
    @Pointcut("execution(public * com.wangyadong.hobby.controller.*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容  
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        LOGGER.info("URL---------- :[{}] " + request.getRequestURL());
        LOGGER.info("HTTP_METHOD---------- : " + request.getMethod());
        LOGGER.info("IP --------: " + request.getRequestURL().toString());
        Object[] args = joinPoint.getArgs();
        LOGGER.info("ARGS------- : " + args.toString());
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getName();
        if ("login".equals(methodName) || "register".equals(methodName)) {
            return;
        }
        WuserInfo session = getSession(request.getSession());
//        if (session == null) {
//            throw new RuntimeException("登录信息已经失效，请重新登录");
//        }

    }


}  
